<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sina_fetch extends CI_Controller {

    const MAX_URLS = 10; // 一次最多10个url
    const ERROR_TAB = 'error';
    private $data = array();

    public function __construct(){
        parent::__construct();
        $this->load->helper('MY_auth_verification');
        $this->load->library('error_code_library');
        $this->load->database();
        $this->_return_data = $this->config->item('return_data');
        $this->load->helper('form');
        $this->load->library('crawl_abstract');
        $this->load->library('crawl_keyword_library');
        $this->load->library('crawl_weibo_info_from_wap_library');
    }

	public function index()
	{
       $this->weibo_page();
    }

    /**
     * 在调用接口前先验证
     */
    function auth_first() {
        $auth_verification_result = authentication($this, $this->input->post());
        if(is_array($auth_verification_result) && array_key_exists(self::ERROR_TAB,$auth_verification_result)) {
            echo json_encode($auth_verification_result);
            exit;
        } else {
            return true;
        }
    }

    /**
     * 显示页面
     */
    public function weibo_comm_page() {
        $urls = $this->input->post('urls');
        $this->data['urls'] = $urls;
        if($urls){
            $urls = $this->crawl_weibo_info_from_wap_library->filter_urls($urls);
            $rs = array();
            foreach($urls as $url){
                $this->crawl_weibo_info_from_wap_library->fetch_weibo_comm($url['url']);
                $weibo_info = $this->crawl_weibo_info_from_wap_library->get_weibo_tree();

                $weibo_info['url'] = $url['url'];
                $rs[] = $weibo_info;
            }
            $this->data['weibo_tree'] = $rs;
        }

        $this->load->view('fetch_weibo_test', $this->data);
    }

    /**
     * 评论测试页面
     */
    function comms_page(){
        $url = $this->input->post('url');
        $page = $this->input->post('page');
        $mid = $this->input->post('mid');
        $this->data['url'] = $url;
        $this->data['mid'] = $mid;
        $this->data['page'] = $page;

        // 如果有url就用url，不然就用mid去获取
        if(!empty($page)){
            if($url){
                // 过滤url
                $url = $this->crawl_weibo_info_from_wap_library->filter_url($url);

                $para = array(
                    'key' => 'url',
                    'val' => $url,
                );
            }elseif($mid){
                $para = array(
                    'key' => 'mid',
                    'val' => $mid,
                );
            }

            $this->crawl_weibo_info_from_wap_library->get_comms($para, $page);

            $this->data['weibo_tree'] = $this->crawl_weibo_info_from_wap_library->get_weibo_tree();
        }
        $this->load->view('fetch_comms_test', $this->data);
    }

    /**
     * 微博信息页面
     */
    function weibo_page(){
        $urls = $this->input->post('urls');
        $this->data['urls'] = $urls;
        if($urls){
            $urls = $this->crawl_weibo_info_from_wap_library->filter_urls($urls);

            $rs = array();
            foreach ($urls as $url) {
                $this->crawl_weibo_info_from_wap_library->fetch_weibo($url['url']);
                $weibo_info = $this->crawl_weibo_info_from_wap_library->get_weibo_tree();

                $weibo['url'] = $url['url'];
                $weibo['reposts_count'] = $weibo_info['weibo_info']->mblog->reposts_count;
                $weibo['comments_count'] = $weibo_info['weibo_info']->mblog->comments_count;
                $weibo['attitudes_count'] = $weibo_info['weibo_info']->mblog->attitudes_count;

                $rs[] = $weibo;
            }

            $this->data['weibo_tree'] = $rs;
        }

        $this->load->view('fetch_rc_counts_test', $this->data);
    }


    /**
     * 获取微博接口
     * 参数： url: 一次1条
     * 返回： weibo_tree 的json 格式
     */
    function get_weibo_comms_info(){
        $this->auth_first();
        $this->load->helper('my_common_helper');
        $url = $this->input->post('url');
        if($url){
            $url = $this->crawl_weibo_info_from_wap_library->filter_url($url);

            if(empty($url)){ // 过滤后没url了
                json_msg(1, 'no url');
            }

            $this->crawl_weibo_info_from_wap_library->fetch_weibo_comm($url);
            $weibo_tree = $this->crawl_weibo_info_from_wap_library->get_weibo_tree();
            if(!empty($weibo_tree['weibo_info'])){
                json_msg(0, $weibo_tree);
            }else{
                json_msg(1, 'please retry a minute late');
            }
        }
        json_msg(1, 'no url');
    }


    /**
     * 获取评论列表
     * 需要post值： weibo_url, page  微博链接和第几页评论
     *
     */
    function get_comms(){
        $this->auth_first();
        $this->load->helper('my_common_helper');
        $url = $this->input->post('url');
        $page = $this->input->post('page');
        $mid = $this->input->post('mid');

        if(empty($page) || (empty($url) && empty($mid))){
            json_msg(1, 'para missing'.json_encode($this->input->post()));
        }

        if(!empty($page)){
            if($url){
                // 过滤url
                $url = $this->crawl_weibo_info_from_wap_library->filter_url($url);

                $para = array(
                    'key' => 'url',
                    'val' => $url,
                );
            }elseif($mid){
                $para = array(
                    'key' => 'mid',
                    'val' => $mid,
                );
            }

            $this->crawl_weibo_info_from_wap_library->get_comms($para, $page);

            $weibo_tree = $this->crawl_weibo_info_from_wap_library->get_weibo_tree();
        }

        if(!empty($weibo_tree['comm'])){
            json_msg(0, $weibo_tree);
        }else{
            json_msg(1, 'please retry a minute late');
        }
        json_msg(1, 'no urls');
    }

    /**
     * 获取转发评论数
     * @param $urls
     */
    function get_weibo_info()
    {
        $this->auth_first();
        $this->load->helper('my_common_helper');
        $url = $this->input->post('url');

        if($url){
            $url = $this->crawl_weibo_info_from_wap_library->filter_url($url);

            if(empty($url)){ // 过滤后没url了
                json_msg(1, 'no url');
            }

            $this->crawl_weibo_info_from_wap_library->fetch_weibo($url);
            $weibo_tree = $this->crawl_weibo_info_from_wap_library->get_weibo_tree();
            if(!empty($weibo_tree['weibo_info'])){
                json_msg(0, $weibo_tree);
            }else{
                json_msg(1, 'please retry a minute late');
            }
        }
        json_msg(1, 'no url');
    }
    
    function get_weibo_info_byid()
    {
    	$this->auth_first();
    	$this->load->helper('my_common_helper');
    	$id = $this->input->post('id');
    	$uid = $this->input->post('uid');
    
    	$url =  "http://weibo.com/{$uid}/".getCodeByMid($id);
    	
    	if($url){
    		$url = $this->crawl_weibo_info_from_wap_library->filter_url($url);
    
    		if(empty($url)){ // 过滤后没url了
    			json_msg(1, 'no url');
    		}
    
    		$this->crawl_weibo_info_from_wap_library->fetch_weibo($url);
    		$weibo_tree = $this->crawl_weibo_info_from_wap_library->get_weibo_tree();
    		if(!empty($weibo_tree['weibo_info'])){
    			json_msg(0, $weibo_tree);
    		}else{
    			json_msg(1, 'please retry a minute late');
    		}
    	}
    	json_msg(1, 'no url');
    }
}
